<?php
require_once("../../inc/global.php");
require_once("functions.php");

$header = new GHeader('Geradores - ' . SYS_DESC);
$header->addScript("functions.js");
$header->show(true);
//--------------------------------------------------------------------

$pasta = $_POST["hdn_pasta"];
$crud = stripslashes($_POST["crud"]);
$form = stripslashes($_POST["form"]);
$class = stripslashes($_POST["class"]);
$classDao = stripslashes($_POST["classDao"]);
$functions = stripslashes($_POST["functions"]);
$grid = stripslashes($_POST["grid"]);
$index = stripslashes($_POST["index"]);
$procedures = stripslashes($_POST["procedures"]);

$print = '';
$flag = false;

if (isset($pasta)) {

    $pastaCrud = ROOT_SYS_ADMIN . $pasta;

    $classPg = ROOT_SYS_CLASS . "/" . $pasta . ".php";
    $classDaoPg = ROOT_SYS_CLASS . "/" . $pasta . "Dao.php";

    $crudPg = $pastaCrud . "/crud_" . $pasta . ".php";
    $formPg = $pastaCrud . "/form_" . $pasta . ".php";
    $functionsPg = $pastaCrud . "/functions.js";
    $gridPg = $pastaCrud . "/grid_" . $pasta . ".php";
    $indexPg = $pastaCrud . "/index.php";
    $proceduresPg = $pastaCrud . "/procedures.sql";

    if (file_exists($pastaCrud)) {
        chmod($pastaCrud, 1777);
        try {
            if (file_exists($crudPg))
                unlink($crudPg);
            if (file_exists($formPg))
                unlink($formPg);
            if (file_exists($classPg))
                unlink($classPg);
            if (file_exists($classDaoPg))
                unlink($classDaoPg);
            if (file_exists($functionsPg))
                unlink($functionsPg);
            if (file_exists($gridPg))
                unlink($gridPg);
            if (file_exists($indexPg))
                unlink($indexPg);
            if (file_exists($proceduresPg))
                unlink($proceduresPg);
            if (file_exists($pastaCrud))
                rmdir($pastaCrud);
            $print = "Arquivos Anteriores excluídos com sucesso!<br/>";
        } catch (GException $exc) {
            $print = "Erro ao excluir arquivos";
            echo $exc->getTraceAsString();
        }
    }
    if (mkdir($pastaCrud, 0777)) {
        try {
            $arquivoCrud = fopen($crudPg, "wb");
            fwrite($arquivoCrud, $crud);
            fclose($arquivoCrud);

            $arquivoForm = fopen($formPg, "wb");
            fwrite($arquivoForm, $form);
            fclose($arquivoForm);

            $arquivoClass = fopen($classPg, "wb");
            fwrite($arquivoClass, $class);
            fclose($arquivoClass);

            $arquivoClassDao = fopen($classDaoPg, "wb");
            fwrite($arquivoClassDao, $classDao);
            fclose($arquivoClassDao);

            $arquivoFunctions = fopen($functionsPg, "wb");
            fwrite($arquivoFunctions, $functions);
            fclose($arquivoFunctions);

            $arquivoGrid = fopen($gridPg, "wb");
            fwrite($arquivoGrid, $grid);
            fclose($arquivoGrid);

            $arquivoIndex = fopen($indexPg, "wb");
            fwrite($arquivoIndex, $index);
            fclose($arquivoIndex);

            $arquivoProcedures = fopen($proceduresPg, "wb");
            fwrite($arquivoProcedures, $procedures);
            fclose($arquivoProcedures);

            $print = "Arquivos criados com sucesso!";
            $flag = true;
        } catch (GException $exc) {
            $print = "Erro ao criar arquivos";
            echo $exc->getTraceAsString();
        }
    } else
        $print = "Erro ao criar pasta";
} else
    $print = "Dados inválidos";

$alert = array();
if ($flag) {
    $alert[0] = 'success';
    $alert[1] = $print;
    $alert[2] = 'Sucesso';
} else {
    $alert[0] = 'error';
    $alert[1] = $print;
    $alert[2] = 'Atenção';
}
?>
<script>
    jAlert('<?= $alert[0]; ?>','<?= $alert[1]; ?>', '<?= $alert[2]; ?>', function(r){
        if (r){
            window.history.back();
        }
    });
</script>
<?
//--------------------------------------------------------------------
$footer = new GFooter();
$footer->show(true);

function recurse_chown_chgrp($mypath, $uid, $gid) {
    $d = opendir($mypath);
    while (($file = readdir($d)) !== false) {
        if ($file != "." && $file != "..") {

            $typepath = $mypath . "/" . $file;

            //print $typepath. " : " . filetype ($typepath). "<BR>" ;
            if (filetype($typepath) == 'dir') {
                recurse_chown_chgrp($typepath, $uid, $gid);
            }

            chown($typepath, $uid);
            chgrp($typepath, $gid);
        }
    }
}
?>

